﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using OAuth;
using Entity;
using DataAccess;
using System.Xml;
using Entity.RSS_Feed;
using System.Diagnostics;

namespace BusinessLogic
{
    /// <summary>
    /// 这个类目的是 封装程序级的一些方法
    /// </summary>
    public class AddinLogic
    {
        /// <summary>
        /// 获取指定 uid的用户广播
        /// 第一步：用oauth 给API url 签名
        /// 第二步：发送请求，获取结果，形式是XML Document
        /// 第三步：xml 解析，构造 实体对象
        /// </summary>
        /// <param name="uid">用户 的uid</param>
        /// <returns></returns>
        public static List<UserNews> GetUserNews(People sender, UserCredential credential)
        {
            if (credential == null) return null;

            string uid = sender.UID;
            if (string.IsNullOrEmpty(uid)) return null;

            List<UserNews> userNews = new List<UserNews>();
            string start_index = "0";     //返回结果的起始索引
            string max_results = "10";      //返回结果的数量
            string broadcastUrl = @"http://api.douban.com/people/" + uid + @"/miniblog?start-index=" + start_index + "&max-results=" + max_results;
            string signedAPIUrl = UserCredentialLogic.SignAPICall(broadcastUrl, UserCredential.ApiKey, UserCredential.ApiKeySecret, credential.AccessToken, credential.AccessTokenSecret);
            
            // Http Request的设置 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(signedAPIUrl);
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = null;
            StreamReader stream = null;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
                //string responseBody = stream.ReadToEnd();

                //TODO: access level
                XmlDocument newsFeed = new XmlDocument();
                newsFeed.Load(stream);

                userNews = DouBanHelper.GetUserNewsFromXML(newsFeed);
            }
            catch (WebException e)
            {
                throw;
            }
            finally
            {
                stream = null;
                response = null;
            }
            return userNews;
        }

        /// <summary>
        /// 获取用户所有的活动
        /// 第一步：用oauth 给API url 签名
        /// 第二步：发送请求，获取结果，形式是XML Document
        /// 第三步：xml 解析，构造 实体对象
        /// </summary>
        /// <param name="?"></param>
        /// <returns>活动列表</returns>
        public static List<OneCityEvent> GetOneCityEvents(People sender, UserCredential credential)
        {
            if (sender == null || credential == null) return null;

            string userID = sender.UID;
            if (string.IsNullOrEmpty(userID)) return null;

            List<OneCityEvent> events = new List<OneCityEvent>();
            string start_index = "1";     //返回结果的起始索引
            string max_results = "10";      //返回结果的数量

            string eventsUrl = @"http://api.douban.com/people/" + userID + @"/events?start-index=" + start_index + "&max-results=" + max_results;
            string signedAPIUrl = UserCredentialLogic.SignAPICall(eventsUrl, UserCredential.ApiKey, UserCredential.ApiKeySecret, credential.AccessToken, credential.AccessTokenSecret);
            
            
            // Http Request的设置 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(signedAPIUrl);
            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = null;
            StreamReader stream = null;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
                stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
                XmlDocument eventsFeed = new XmlDocument();
                eventsFeed.Load(stream);

                events = DouBanHelper.GetOneCityEventFromXML(eventsFeed);

            }
            catch (WebException e)
            {
                throw;
            }
            finally
            {
                response = null;
                stream = null;
            }
            return events;
        }

        
    }
}
